﻿
namespace SuperGo.SilverlightLib {
    public class Intersection {
        private const string LETTERS = "ABCDEFGHJKLMNOPQRSTUVWXYZ";

        private readonly int _x;
        private readonly int _y;

        public Intersection(int x, int y) : this(x, y, StoneState.Empty) {
        }

		public Intersection(int x, int y, StoneState state) {
			_x = x;
			_y = y;
			State = state;
		}

        public StoneState State { get; set; }

        public char Letter {
            get { return GetLetter(_x); }
        }

		public static char GetLetter(int x) {
			return LETTERS[x];
		}

        public int X {
            get { return _x; }
        }

        public int Y {
            get { return _y; }
        }

        /// <summary>
        /// {arse a location in the form of [LETTER][NUMBER] into the x,y coordinates.
        /// </summary>
        /// <param name="x">The zero based X location</param>
        /// <param name="y">The zero based Y location</param>
        public static void Parse(string location, out int x, out int y) {
            if (!TryParse(location, out x, out y)) {
                throw new GoException(string.Format("Invalid board location {0}", location));
            }
        }

        /// <summary>
        /// Tries to parse a location in the form of [LETTER][NUMBER] into the x,y coordinates.
        /// </summary>
        /// <param name="x">The zero based X location</param>
        /// <param name="y">The zero based Y location</param>
        /// <returns>True if the parse was a success.</returns>
        public static bool TryParse(string location, out int x, out int y) {
            location = location.ToUpper();
            x = y = 0;
            if (location.Length == 2 || location.Length == 3) {
                location = location.ToUpper();
                x = LETTERS.IndexOf(location[0]);
                if (x >= 0 && int.TryParse(location.Substring(1), out y)) {
                    y--;
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Formats an x,y coordinate into [LETTER][NUMBER].
        /// </summary>
        /// <param name="x">The zero based X location</param>
        /// <param name="y">The zero based Y location</param>
        public static string ToString(int x, int y) {
            return string.Format("{0}{1}", LETTERS[x], y + 1);
        }

		public override bool Equals(object obj) {
			Intersection intersection = obj as Intersection;
			if (intersection == null)
				return base.Equals(obj);
			return intersection.X == X 
				&& intersection.Y == Y 
				&& intersection.State == State;
		}

		public override int GetHashCode() {
			return (X * 19) + Y;
		}

        public Intersection Clone() {
            Intersection newIntersection = new Intersection(X, Y);
            newIntersection.State = State;
            return newIntersection;
        }
    }
}
